package com.idea.zd.sys.util;

import com.aspose.words.Document;
import com.aspose.words.License;
import com.aspose.words.SaveFormat;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.*;

/**
 * word转pdf
 * @author zzy
 * @Date 2019-07-18 15:25
 */
public class Word2PdfUtil {

    private static Logger logger = LoggerFactory.getLogger(Word2PdfUtil.class);

    private static final String LICENSE = "<License>  \n" +
            "  <Data>  \n" +
            "    <Products>  \n" +
            "      <Product>Aspose.Total for Java</Product>  \n" +
            "      <Product>Aspose.Words for Java</Product>  \n" +
            "    </Products>  \n" +
            "    <EditionType>Enterprise</EditionType>  \n" +
            "    <SubscriptionExpiry>20991231</SubscriptionExpiry>  \n" +
            "    <LicenseExpiry>20991231</LicenseExpiry>  \n" +
            "    <SerialNumber>8bfe198c-7f0c-4ef8-8ff0-acc3237bf0d7</SerialNumber>  \n" +
            "  </Data>  \n" +
            "  <Signature>sNLLKGMUdF0r8O1kKilWAGdgfs2BvJb/2Xp8p5iuDVfZXmhppo+d0Ran1P9TKdjV4ABwAgKXxJ3jcQTqE/2IRfqwnPf8itN8aFZlV3TJPYeD3yWE7IT55Gz6EijUpC7aKeoohTb4w2fpox58wWoF3SNp6sK6jDfiAUGEHYJ9pjU=</Signature>  \n" +
            "</License> ";


    static {
        // 验证License 若不验证则转化出的pdf文档会有水印产生
        getLicense();
    }

    public static void main(String[] args) {
    	String url="/Users/yangzhenze/Documents/宁德天气/微信文件/决策/宁德-重要天气预警报告（台风）-2019037.doc";
        Word2PdfUtil x = new Word2PdfUtil();
        doc2pdf(url,url.replace(".doc", ".pdf"));
    }

    /**
     * 验证License 若不验证则转化出的pdf文档会有水印产生
     * @return
     */
    public static boolean getLicense() {
        boolean result = false;
        try {
            //InputStream is = Test.class.getClassLoader().getResourceAsStream("license.xml"); // license.xml应放在..\WebRoot\WEB-INF\classes路径下
            InputStream is = new ByteArrayInputStream(LICENSE.getBytes());
            License aposeLic = new License();
            aposeLic.setLicense(is);
            result = true;
        } catch (Exception e) {
            logger.error("转化pdf失败，验证License异常", e);
        }
        return result;
    }

    public static boolean doc2pdf(String inPath, String outPath) {
        FileOutputStream os=null;
        try {
            long old = System.currentTimeMillis();
            // 新建一个空白pdf文档
            File file = new File(outPath);
            os = new FileOutputStream(file);
            // Address是将要被转化的word文档
            Document doc = new Document(inPath);
            // 全面支持DOC, DOCX, OOXML, RTF HTML, OpenDocument, PDF,
            doc.save(os, SaveFormat.PDF);
            // EPUB, XPS, SWF 相互转换// 转化用时
            System.out.println("word转化pdf共耗时：" + ((System.currentTimeMillis() - old) / 1000.0) + "秒");
            return true;
        } catch (Exception e) {
            logger.error("转化pdf失败", e);
            e.printStackTrace();
        }finally {
			if (os!=null) {
				try {
					os.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
        return false;
    }
}
